package com.ice.tools;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ReceiveWechatByHttp {
        /**
         * 通过url获取网络请求的信息
         * @param url
         * @return
         */
		public static Map  receive(String url){
           //生成httpclient
			HttpClient httpClient = new DefaultHttpClient();
            //get请求
			HttpGet httpGet = new HttpGet(url);
            Map<String, Object> map = null;
			try {
                //httpclient执行get请求，并获得请求结果
				HttpResponse httpResponse = httpClient.execute(httpGet);
                
				HttpEntity httpEntity = httpResponse.getEntity();
				String responseContent = EntityUtils.toString(httpEntity,"utf-8");
                
                //这里使用jackson 将网络返回的对像转化为map对象
				ObjectMapper objectMapper = new ObjectMapper();
				 if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
					 map = objectMapper.readValue(responseContent,Map.class);
				 }
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				httpClient.getConnectionManager().shutdown();
			}
			return map;
		}
}
